package com.flying.acls.domain.resource;

import com.flying.acls.domain.context.ContextStringValueResolver;
import com.flying.acls.domain.context.ExpressionContext;
import com.flying.acls.model.ExpressionType;
import com.flying.acls.model.Resource;
import lombok.Setter;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Setter
public class DOPatternBasedResourceBuilder {
    private Map<String, DOPatternInfo> doPatternInfos;
    private ContextStringValueResolver stringResolver;

    public DOPatternBasedResourceBuilder(ContextStringValueResolver stringResolver, Map<String, DOPatternInfo> doPatternInfos) {
        this.stringResolver = stringResolver;
        this.doPatternInfos = doPatternInfos;
    }

    public boolean support(String className) {
        return doPatternInfos.containsKey(className);
    }

    public List<Resource> build(ExpressionContext context, String className) {
        List<String> patterns = doPatternInfos.get(className).getPatterns();
        return stringResolver.resolveStringValue(context, patterns)
                .stream()
                .map(expression -> ResourceFactory.get(context.getResourceType(), ExpressionType.DIRECT, expression))
                .collect(Collectors.toList());
    }
}
